// components/goodsList/goodsList.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    hotList: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //跳转产品详情
    goGoodsDetail(e) {
      let that = this
      let detail = {
        id: e.currentTarget.dataset.id
      }
      let option = {}
      that.triggerEvent('goGoodsDetail', detail, option)
    },
    //加入购物车
    addCart(e) {
      let that = this
      let item=e.currentTarget.dataset.item
      if (item.fTotalCount>0){
        let detail = {
          id: e.currentTarget.dataset.id,
          count: e.currentTarget.dataset.count
        }
        let option = {}
        that.triggerEvent('openCartModal', detail, option)
      }else{
        wx.showToast({
          title: '库存不足',
          icon:'none'
        })
      }
    },
    //计算价格
    calTotalPri(){
      let that=this
      let temList=that.properties.hotList
      temList.forEach(val=>{
        val.totalPri = val.fFixedPrice*val.count
        val.totalPriTxt=val.totalPri.toFixed(2)
      })
      that.setData({
        hotList:temList
      })
    },
    //设置数量
    setCount(e) {
      let that = this,
        type = e.currentTarget.dataset.type,
        id = e.currentTarget.dataset.id
      let temList=that.properties.hotList
      switch(type){
        case 'minus':
          temList.forEach(val => {
            if (val.fId === id) {
              if(val.count>1){
                val.count=--val.count
                that.calTotalPri()
              }
            }
          })
          break
        case 'plus':
          temList.forEach(val=>{
            if (val.fId === id) {
              if (val.count < val.fTotalCount) {
                val.count = ++val.count
                that.calTotalPri()
              } else {
                wx.showToast({
                  title: '没有库存啦',
                  icon: 'none'
                })
              }
            }
          })
          break
      }
      that.setData({
        hotList:temList
      })
      
    }
  },

  ready() {
    let that = this
    that.calTotalPri()
  }

})
